package com.million.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.million.commonutils.JwtUtils;
import com.million.commonutils.orderVo.UcenterMemberVo;
import com.million.educenter.entity.UcenterMember;
import com.million.educenter.entity.params.vo.LoginParams;
import com.million.educenter.entity.params.vo.RegisterVo;
import com.million.educenter.mapper.UcenterMemberMapper;
import com.million.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.million.servicebase.exception.GuliException;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 十麓
 * @since 2022-04-03
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //加密盐
    private static final String slat = "shiluyyds!@#";


    //登录
    @Override
    public String loginUser (LoginParams loginParams) {

        /**
         * 思路：
         * 1.先获取手机号和密码，是否为空
         * 2.手机号是否正确
         * 3.密码是否正确
         * 4.是否被禁用
         * 5.登录成功，生成token
         * 6.写入到redis中
         */
        // 1.先获取手机号和密码，是否为空
        String mobile = loginParams.getMobile();
        String password = loginParams.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登录失败");
        }
        //2.手机号是否正确
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(queryWrapper);
        if (ucenterMember == null) {  //没有这个手机号
            throw new GuliException(20001, "登录失败");
        }
        //3.密码是否正确
        //先将传过了的密码进行加密，再到数据库中查找,我用的是md5 加密盐
        password = DigestUtils.md5Hex(password + slat);
        if (!password.equals(ucenterMember.getPassword())) {
            throw new GuliException(20001, "登录失败");
        }

        //4.是否被禁用 1是true ，0 是false
        if (ucenterMember.getIsDisabled()) {
            throw new GuliException(20001, "登录失败");
        }

        //登录成功，生成token ,用jwt
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        //写入到redis中
//        redisTemplate.opsForValue().set("token_" + token, JSON.toJSONString(ucenterMember), 1, TimeUnit.DAYS);
        return token;
    }

    //登录
    @Override
    public void registerUser (RegisterVo registerVo) {
        //先获取注册的数据
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code)) {
            throw new GuliException(20001, "注册失败");
        }

        //如何根据当前的验证码和redis 中的验证码比较是否相同
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new GuliException(20001, "注册失败");
        }

        //判断手机号是否重复，表里面存在相同的手机号不能注册
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count > 0) { //说明有
            throw new GuliException(20001, "注册失败");
        }
        //到这里就支持成功
        //存入到数据库中
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(DigestUtils.md5Hex(password + slat));
        member.setIsDisabled(false); //不禁用
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(member);
        //生成token
//        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        //存入到redis中
//        redisTemplate.opsForValue().set("token_" + token, JSON.toJSONString(member), 1, TimeUnit.DAYS);
    }

    //根据openid 获取UcenterMember
    @Override
    public UcenterMember getMemberByOpenId (String openid) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        return member;
    }

    //根据用户id 查询用户信息
    @Override
    public UcenterMemberVo getMemberInfoById (String memberId) {
        UcenterMemberVo ucenterMemberVo = new UcenterMemberVo();
        UcenterMember member = baseMapper.selectById(memberId);
        BeanUtils.copyProperties(member,ucenterMemberVo);
        return ucenterMemberVo;
    }

    //根据创建时间查询注册人数
    @Override
    public Integer getRegisterUcenter (String day) {
        return baseMapper.getRegisterUcenter(day);
    }
}
